<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row :gutter="20">
        <!-- 搜索框 -->
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="input3" clearable @clear="add">
            <el-button slot="append" icon="el-icon-search" @click="checkData" ></el-button>
          </el-input>
        </el-col>
        <!-- 添加商品 -->
        <el-col :span="4">
          <el-button type="primary" @click="pageAdd">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表表格 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="序号" width="50px"></el-table-column>
        <!-- 商品名称 -->
        <el-table-column label="商品名称" prop="goods_name" width="650px">

        </el-table-column>
        <!-- 商品价格 -->
        <el-table-column label="商品价格(元)" prop="goods_price" width="120px">

        </el-table-column>
        <!-- 商品重量 -->
        <el-table-column label="商品重量" prop="goods_weight" width="100px">

        </el-table-column>
        <!-- 创建时间 -->
        <el-table-column label="创建时间">
          <template slot-scope="scoped">
            {{scoped.row.add_time | date}}
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" width="177px">
          <template slot-scope="scoped">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="delGoodsData(scoped.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5, 8, 9, 10]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 商品列表数据参数
      queryInfo: {
        // 查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 5
      },
      // 搜索框输入的数据
      input3: '',
      // 接收商品列表数据
      goodsList: [],
      // 商品列表的总数量
      total: 0
    }
  },
  methods: {
    // 获取商品列表的数据
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.$message.success('获取数据成功')
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 每页展示的触发事件
    handleSizeChange (newPage) {
      this.queryInfo.pagesize = newPage
      this.getGoodsList()
    },
    // 当前页的触发事件
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 搜索数据的点击按钮
    checkData () {
      this.queryInfo.query = this.input3
      this.getGoodsList()
    },
    // 搜索表单的清空事件
    add () {
      this.queryInfo.query = ''
      this.getGoodsList()
    },
    // 删除商品事件
    async delGoodsData (row) {
      const con = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      if (con) {
        const { data: res } = await this.$http.delete('goods/' + row.goods_id)
        if (res.meta.status !== 200) return this.$message.error('删除失败')
        this.$message.success('删除成功')
        this.getGoodsList()
        console.log(row.goods_id)
      }
    },
    // 添加商品的路由导航点击事件
    pageAdd () {
      this.$router.push('/home/goods/add')
    }
  },
  mounted () {
    this.getGoodsList()
  }
}
</script>

<style>
</style>
